# This file is part of the Net::Tube module.
#
# Net::Tube is Free Software; you can use it, redistribute it and/or
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
#    Foundation, either version 2, or (at your opinion) any later
#    version, or
#
# b) the "Artistic License" which comes with Perl.
#
package Net::Tube::Loader;

use Net::Tube;

our @ISA = qw(Net::Tube::Filter);

sub process {
    my $self = shift;
    my $bucket = shift;
    
    $self->SUPER::process($bucket);
    
    my @srcs = $self->check_bucket($bucket);
    unless ( scalar @srcs ) {
        @srcs = $self->check_config();
    }
    
    my $retval = $self->create_bucket();
    foreach my $src (@srcs){
        $self->load($retval, $src);
    }
    return $retval;
}

sub check_bucket {
    my $self = shift;
    my $bucket = shift;
    
    my @xps = $self->get_config_entry("loadXPath");
     
    my @retval = ();
    if ( scalar @xps ) {
        foreach my $xp (@xps) {
            my $tvs = $bucket->find_nodes($xp);
            if ( defined $tvs && scalar @$tvs ) {
                push @retval, @$tvs;
            } 
        } 
    }
    
    return @retval;
}

sub check_config {
    my $self = shift;
    my @retval = $self->get_config_entry("loadSource");
    if (scalar @retval) {
        return @retval;
    }
    return ();
}

sub add_to_bucket {
    my $self   = shift;
    my $bucket = shift;
    my $data   = shift;
    my $url    = shift;
		   
    if ( defined $data ) {    
        eval { 
	    $bucket->add_xml($data, $url); 
	    
	};
        if ($@) {
            eval { $bucket->add_json($data); };
	    if($@) {
		die 'INVALID INPUT DATA';
	    }
        }
    }
}

sub load {}

1;
